<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="广告标题" prop="bannerTitle">
        <el-input
          v-model="queryParams.bannerTitle"
          placeholder="请输入广告标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['mall:banner:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mall:banner:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['mall:banner:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['mall:banner:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="bannerList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="广告ID" align="center" prop="bannerId"/>
      <el-table-column label="广告标题" align="center" prop="bannerTitle"/>
      <el-table-column label="广告类型" align="center" prop="bannerType">
        <template slot-scope="scope">
          <span v-if="scope.row.bannerType === '0'">首页图片</span>
          <span v-if="scope.row.bannerType === '1'">首页弹窗</span>
        </template>
      </el-table-column>
      <el-table-column label="广告图片" align="center" prop="bannerUrl" width="100">
        <template slot-scope="scope">
          <image-preview v-if="scope.row.bannerUrl" :src="scope.row.bannerUrl" :width="80" :height="45"
                         style="box-shadow: 0 0 5px 5px #aaaaaa;margin-top:10px"/>
        </template>
      </el-table-column>
      <el-table-column label="广告链接类型" align="center" prop="bannerLinkType">
        <template slot-scope="scope">
          <span v-if="scope.row.bannerLinkType === '0'">菜单</span>
          <span v-if="scope.row.bannerLinkType === '1'">指定网页</span>
          <span v-if="scope.row.bannerLinkType === '2'">商品详情</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderNum" label="排序" align="center"></el-table-column>
      <el-table-column label="是否停用" align="center" prop="bannerOn">
        <template slot-scope="scope">
          <span v-if="scope.row.bannerOn === '0'">启用</span>
          <span v-if="scope.row.bannerOn === '1'">停用</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="200">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mall:banner:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mall:banner:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改咖啡滚动广告对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="广告标题" prop="bannerTitle">
          <el-input v-model="form.bannerTitle" placeholder="请输入广告标题"/>
        </el-form-item>
        <el-form-item label="广告类型" prop="bannerType">
          <el-radio-group v-model="form.bannerType">
            <el-radio :label="'0'">首页图片</el-radio>
            <el-radio :label="'1'">首页弹窗</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="广告图片">
          <imageUpload v-model="form.bannerUrl"/>
        </el-form-item>
        <!--        <el-form-item label="广告内容" v-if="form.adType === '1'">-->
        <!--          <editor v-model="form.adContent" :min-height="192"/>-->
        <!--        </el-form-item>-->
        <el-form-item label="广告链接类型" prop="bannerLinkType" v-if="form.bannerType === '0'">
          <el-radio-group v-model="form.bannerLinkType">
            <el-radio :label="'0'">链接菜单</el-radio>
            <el-radio :label="'1'">指定链接</el-radio>
            <el-radio :label="'2'">商品详情</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="指定链接地址" prop="bannerLink"
                          v-if="form.bannerType === '0' && form.bannerLinkType === '1'">
              <el-input v-model="form.bannerLink" placeholder="请输入指定链接地址"/>
            </el-form-item>
            <el-form-item label="商品编码" prop="bannerLink" v-if="form.bannerType === '0' && form.bannerLinkType === '2'">
              <el-input v-model="form.bannerLink" placeholder="请输入商品编码"/>
            </el-form-item>
            <el-form-item label="指定菜单" prop="bannerLink" v-if="form.bannerType === '0' && form.bannerLinkType === '0'">
              <el-radio-group v-model="form.bannerLink">
                <el-radio
                  v-for="dict in dict.type.mall_wx_menu"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input-number v-model="form.orderNum" placeholder="请输入显示顺序" controls-position="right" :min="1"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="是否停用">
          <el-radio-group v-model="form.bannerOn">
            <el-radio :label="'0'">启用</el-radio>
            <el-radio :label="'1'">停用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addBanner, delBanner, exportBanner, getBanner, listBanner, updateBanner} from "@/api/mall/banner";
import ImageUpload from '@/components/ImageUpload';

export default {
  name: "Banner",
  dicts: ['mall_wx_menu'],
  components: {
    ImageUpload,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 咖啡滚动广告表格数据
      bannerList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        bannerTitle: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询咖啡滚动广告列表 */
    getList() {
      this.loading = true;
      listBanner(this.queryParams).then(response => {
        this.bannerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        bannerId: null,
        bannerTitle: null,
        bannerUrl: null,
        bannerType: null,
        bannerContent: null,
        bannerLinkType: null,
        bannerLink: null,
        orderNum: null,
        bannerOn: '0',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.adId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加首页广告";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const bannerId = row.bannerId || this.ids
      getBanner(bannerId).then(response => {
        this.form = response.data;
        console.log(this.form)
        this.form.bannerOn = response.data.bannerOn
        this.open = true;
        this.title = "修改首页广告";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.bannerId != null) {
            updateBanner(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBanner(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const bannerIds = row.bannerId || this.ids;
      this.confirm('是否确认删除咖啡滚动广告编号为"' + bannerIds + '"的数据项？').then(function () {
        return delBanner(bannerIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有首页广告数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return exportBanner(queryParams);
      }).then(response => {
        this.download(response.msg);
      })
    },
    beforeAvatarUpload(file) {
      const isLt5M = file.size / 1024 / 1024 < 5;
      if (!isLt5M) {
        this.$message.error('上传图片大小不能超过 5MB!');
      }
      return isLt5M;
    },
    uploadOSS(file) {
      const _this = this;
      let fileName = file.file.name;
      fileName = Date.now() + this.getUUID() + fileName;
      aliClient().put(fileName, file.file, {
        progress: function (p) {
        }
      }).then(result => {
        _this.form.adUrl = result.url;
      });
    },
    // 随机名称
    getUUID() {
      return `${this.str4()}${this.str4()}-${this.str4()}-${this.str4()}-${this.str4()}-${this.str4()}${this.str4()}${this.str4()}`
    },
    str4() {
      return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1)
    },
    bannerTypeChange(e) {
      console.log(e)
      this.bannerType = e
    }
  }
};
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
